﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(MapGraphNode))]
public class MapGraphNodeEditor : Editor {

    MapGraphNode MapGraphNode;

    public void OnEnable()
    {
        MapGraphNode = target as MapGraphNode;
    }

    public override void OnInspectorGUI()
    {
        
        

        for (int i = 0; i < MapGraphNode.MapLinkNodes.Count; i++)
        {
            EditorGUILayout.LabelField("相邻节点" + i);
            EditorGUILayout.BeginHorizontal();
            var node = EditorGUILayout.ObjectField(MapGraphNode.MapLinkNodes[i].node, typeof(MapGraphNode), true) as MapGraphNode;
            var link = EditorGUILayout.Toggle("是否可通行", MapGraphNode.MapLinkNodes[i].directLinked);
            EditorGUILayout.EndHorizontal();
            if (node==null || node == MapGraphNode) continue;
            if (!node.MapLinkNodes.Exists(v => v.node == MapGraphNode))
            {
                node.MapLinkNodes.Add(new MapLinkNode() { directLinked = link, node = MapGraphNode });
            }
            else
            {
                int otherLinkId = node.MapLinkNodes.FindIndex(v => v.node == MapGraphNode);
                node.MapLinkNodes[otherLinkId] = new MapLinkNode() { directLinked = link,node = MapGraphNode};
            }
            MapGraphNode.MapLinkNodes[i] = new MapLinkNode() { directLinked = link, node = node};
            

            if(GUILayout.Button(new GUIContent("移除节点")))
            {
                MapGraphNode.MapLinkNodes[i].node.MapLinkNodes.RemoveAt(
                MapGraphNode.MapLinkNodes[i].node.MapLinkNodes.FindIndex(v => v.node = MapGraphNode)
                );
                MapGraphNode.MapLinkNodes.RemoveAt(i);
                continue;
            }
        }

        if (GUILayout.Button(new GUIContent("增加节点")))
        {
            MapGraphNode.MapLinkNodes.Add(new MapLinkNode());
        }
        base.OnInspectorGUI();
    }
}
